//
//  ViewController.m
//  DocConvert
//
//  Created by 克里斯龙 on 2024/8/15.
//

#import "ViewController.h"
#import "DocumentConverter.h"
#import "Converter.h"
#import "DocConvert-Swift.h"
#import "SDWebImage/SDWebImageDownloader.h"
@interface ViewController ()
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) NSString *content;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) CADisplayLink *displayLink;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
//    UIImageView * iv = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200,300)];
//    
//    [self.view addSubview:iv];
//    
//   UIImage * image = [self convertUrlToImage:@"https://www.yanqueai.com/vilook/icon/IT.png"];
//    iv.image = image;
//    
    NSString * iconUrl = @"https://inews.gtimg.com/om_bt/OBZ9V9t_v74oB-emxgHJLWDj61tuY2fDklKgdGj9lex3UAA/641";
    

    
//    [self test4];
//    [self demo];
    [self sdktest];
}



- (void)sdktest{
    self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
        self.textView.font = [UIFont systemFontOfSize:18];
        self.textView.editable = NO;
        self.textView.scrollEnabled = YES;
        [self.view addSubview:self.textView];
        
        self.content = @"这是需要逐字逐行打印的文字内容。\n 让我们来实现它。";
        self.currentIndex = 0;

        [self startPrinting];

}

- (void)startPrinting {
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(printNextCharacter)];
    self.displayLink.preferredFramesPerSecond = 10; // 控制打印速度
    [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)printNextCharacter {
    if (self.currentIndex >= self.content.length) {
        [self.displayLink invalidate];
        self.displayLink = nil;
        return;
    }

    NSRange range = NSMakeRange(self.currentIndex, 1);
    NSString *nextCharacter = [self.content substringWithRange:range];
    self.textView.text = [self.textView.text stringByAppendingString:nextCharacter];
    
    self.currentIndex += 1;
}

- (void)test4{
    Converter * converter = [[Converter alloc]init];
    NSString * inputFilePath = [[NSBundle mainBundle]pathForResource:@"pdfTest" ofType:@"docx"];

    [converter convertFileAtPath:inputFilePath outputFormat:@"pdf" completion:^(NSString *outputFilePath, NSError *error) {
        if (error) {
            NSLog(@"File conversion failed: %@", error);
        } else {
            NSLog(@"File converted successfully: %@", outputFilePath);
        }
    }];

}
- (void)demo{
    NSURL * documentURL = [NSBundle.mainBundle URLForResource:@"pdfTest.docx" withExtension:nil];
    //    NSURL *documentURL = [NSURL URLWithString:@"https://example.com/documents/report.docx"];
    
    DocumentConverter *converter = [[DocumentConverter alloc] init];
    [converter convertDocumentFromURL:documentURL
                             toFormat:@"pdf"
                           completion:^(NSURL *convertedDocumentURL, NSError *error) {
        if (error) {
            NSLog(@"Error converting document: %@", error.localizedDescription);
        } else {
            NSLog(@"Converted document URL: %@", convertedDocumentURL);
        }
    }];
}


- (void)test3{
    NSString * inputFilePath = [[NSBundle mainBundle]pathForResource:@"pdfTest" ofType:@"docx"];
    DocumentConverter *converter = [[DocumentConverter alloc] init];
    [converter convertDOCXToPDF:inputFilePath];
}

- (void)test2{
    NSString * inputFilePath = [[NSBundle mainBundle]pathForResource:@"pdfTest" ofType:@"docx"];
    NSString *outputFormat = @"pdf";
    DocumentConverter *converter = [[DocumentConverter alloc] init];
    [converter convertDocument:inputFilePath toFormat:outputFormat complete:^(BOOL success, NSString *outputFilePath, NSError *error) {
        if (success) {
            NSLog(@"File converted successfully. Output file: %@", outputFilePath);
        } else {
            NSLog(@"Failed to convert file: %@", error.localizedDescription);
        }
    }];
}
- (UIImage *)convertUrlToImage:(NSString *)urlSting{
    //    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlSting] options:NSDataReadingMappedIfSafe error:nil];
    //        NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlSting]];
    UIImage * image = [UIImage imageWithData:data];
    //       return image;
    //    });
    return image;
}



@end
